﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace M1
{
    public class 人类移动 : MonoBehaviour
    {
        private 人类状态 cur状态;
        private 人类状态 Cur状态
        {
            get
            {
                return cur状态;
            }
            set
            {
                if (cur状态 != value)
                {
                    cur状态 = value;
                    switch (cur状态)
                    {
                        case 人类状态.休息:
                            anim.SetBool("Run",true);
                            break;
                        case 人类状态.Run:
                            anim.SetBool("Run",false);
                            break;
                    }
                }
            }
        }

        public float velocity = 5;
        public float turnSpeed = 10;
        Animator anim;

        Vector2 input;
        float angle;

        Quaternion targetRotation;
        public Transform cam; //Transform cam;

        public 摄像机跟随 ft;

        void Start()
        {
            // //cam = Camera.main.transform;
            // if (cam.GetComponent<摄像机跟随>())
            // {
            //     ft = cam.GetComponent<摄像机跟随>();
            // }
        }

        void Update()
        {
            GetInput();
            检测并移动(input);
        }

        void 检测并移动(Vector2 输入)
        {
            if (Mathf.Abs(输入.x) < 1 && Mathf.Abs(输入.y) < 1) return;

            CalculateDirection(输入);
            Rotate(输入);
            Move(输入);
        }
        

        public void 摇杆输入移动(Vector2 输入)
        {
            检测并移动(输入);
        }

        void GetInput()
        {
            input.x = Input.GetAxisRaw("Horizontal");
            input.y = Input.GetAxisRaw("Vertical");
        }

        void CalculateDirection(Vector2 输入)
        {
            angle = Mathf.Atan2(输入.x, 输入.y);
            angle = Mathf.Rad2Deg * angle;
            angle += cam.eulerAngles.y;
        }

        void Rotate(Vector2 输入)
        {
            if (ft != null && ft.camRotation)
            {
                transform.rotation = Quaternion.Euler(0, 输入.x * 1.5f, 0) * transform.rotation;
            }
            else
            {
                targetRotation = Quaternion.Euler(0, angle, 0);
            }

            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
        }

        void Move(Vector2 输入)
        {
            transform.position += transform.forward * velocity * Time.deltaTime;
        }
    }
    
    
}
